package com.hyb.algorithm.data.struct.list;

/**
 * @author: ybhu
 * @create: 2021/09/20 09:55
 * <p>
 * 24. 两两交换链表中的节点
 * https://leetcode-cn.com/problems/swap-nodes-in-pairs/
 *
 * 解题思路
 * https://leetcode-cn.com/problems/swap-nodes-in-pairs/solution/liang-liang-jiao-huan-lian-biao-zhong-de-jie-di-91/
 *
 */
public class SwapNodesInPairs {

    public ListNode swapPairs(ListNode head) {

        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode curr = dummyHead;
        while (curr.next != null && curr.next.next != null) {
            ListNode node1 = curr.next;
            ListNode node2 = curr.next.next;

            curr.next = node2;
            node1.next = node2.next;
            node2.next = node1;

            curr = node1;
        }

        return dummyHead.next;


    }
}
